package com.yunji.aggregate.service;

import com.yunji.aggregate.cache.AggregateCache;
import com.yunji.aggregate.convert.AggregateParamConvert;
import com.yunji.aggregate.convert.StatisticsDataConvert;
import com.yunji.aggregate.mapper.IBaseAggregateMapper;
import com.yunji.aggregate.model.dto.analysis.*;
import com.yunji.aggregate.model.dto.base.*;
import com.yunji.aggregate.model.dto.stock.StatisticsDataParam;
import com.yunji.aggregate.model.vo.analysis.*;
import com.yunji.aggregate.model.vo.base.*;
import com.yunji.common.enums.common.UserType;
import com.yunji.common.model.dto.LoginInfo;
import com.yunji.common.utils.bean.BeanUtils;
import com.yunji.system.context.SystemContext;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author : peter-zhu
 * @date : 2024/12/6 15:27
 * @description : 基础数据聚合查询
 **/
@Service
public class BaseAggregateService {

    @Resource
    private IBaseAggregateMapper aggregateMapper;


    public List<BelongStatisticsVO> belongList(BelongParam belong) {
        return aggregateMapper.belongList(belong);
    }

    public List<AgentVO> agentList(AgentParam agentParam) {
        return aggregateMapper.agentList(agentParam);
    }

    /**
     * @param param 过滤信息
     * @Title agentSalesStats
     * @Desc 代理销售列表
     * @Date 2024-09-09 19:49:36.948
     * @Return List<AgentSalesVo>
     */
    public List<AgentSalesVO> agentSalesList(StatisticsDataParam param) {
        StatisticsDataConvert.paramTime(param);
        List<AgentSalesVO> list = aggregateMapper.agentScalesList(param);
        return BeanUtils.isNotNull(list) ? list : new ArrayList<>();
    }

    /**
     * @param param 过滤信息
     * @Title agentEarnsList
     * @Desc 代理收益列表
     * @Date 2024-09-10 10:36:07.722
     * @Return List<AgentEarnsVO>
     */
    public List<AgentEarnsVO> agentEarnsList(StatisticsDataParam param) {
        StatisticsDataConvert.paramTime(param);
        List<AgentEarnsVO> list = aggregateMapper.agentEarnsList(param);
        return BeanUtils.isNotNull(list) ? list : new ArrayList<>();
    }

    public List<MemberDTO> memberList(MemberDTO param) {
        return aggregateMapper.memberList(param);
    }


    /**
     * @Title selectUsableSites
     * @Desc 获取代理商下的网点
     * @Date 2024-08-23 14:42:09.426
     * @Return List<SiteVO> 网点信息
     */
    public List<SiteStatisticsVO> siteList() {
        LoginInfo user = SystemContext.getUser();
        Long belongId = user.getBelongId();
        String userType = user.getUserType();
        if (BeanUtils.equals(userType, UserType.AGENT.getCode())) {
            return Collections.emptyList();
        }
        SiteParam param = new SiteParam();
        param.setParentId(belongId);
        return aggregateMapper.siteList(param);
    }

    public List<SiteStatisticsVO> siteList(SiteParam param) {
        //   List<Long> siteIds = ConvertDataUtil.list2FieldList(voList, SiteStatisticsVO::getId);
//        Map<Long, List<SiteScaleVO>> siteScaleMap = ConvertDataUtil.getGroupBy(() ->
//                aggregateMapper.listScaleBySiteIds(siteIds), SiteScaleVO::getSiteId);
//        for (SiteStatisticsVO siteVO : voList) {
//            siteVO.setSiteScaleList(siteScaleMap.get(siteVO.getId()));
//        }
        return aggregateMapper.siteList(param);
    }

    public List<SiteSalesStatisticsVO> siteSalesList(StatisticsDataParam param) {
        StatisticsDataConvert.paramTime(param);
        return aggregateMapper.siteSalesList(param);
    }

    public List<SiteEarnsStatisticsVO> siteEarnsList(StatisticsDataParam param) {
        StatisticsDataConvert.paramTime(param);
        return aggregateMapper.siteEarnsList(param);
    }

    public List<StaffStatisticsVO> staffList(StaffParam param) {
        return aggregateMapper.staffList(param);
    }

    public List<LinkageQueryVO> containerLinkageQuery(LinkageQueryParam param) {
        return aggregateMapper.containerLinkageQuery(param);
    }

    public List<SiteLocationStatisticsVO> getSiteLocationVOList(SiteLocationStatisticsParam param) {
        List<SiteLocationStatisticsVO> voList = aggregateMapper.getSiteLocationVOList(param);
        for (SiteLocationStatisticsVO vo : voList) {
            vo.setSiteName(AggregateCache.getSiteName(vo.getSiteId()));
            vo.setAgentName(AggregateCache.getAgentName(vo.getAgentId()));
            vo.setLocationTypeName(AggregateCache.getLocationTypeName(vo.getLocationTypeId()));
        }
        return voList;
    }

    public List<LocationSalesStatsVO> locationSalesStats(LocationSalesStatsParam param) {
        List<LocationSalesStatsVO> voList = aggregateMapper.locationSalesStats(param);
        for (LocationSalesStatsVO vo : voList) {
            vo.setSiteName(AggregateCache.getSiteName(vo.getSiteId()));
            vo.setAgentName(AggregateCache.getAgentName(vo.getAgentId()));
            vo.setLocationTypeName(AggregateCache.getLocationTypeName(vo.getLocationTypeId()));
        }
        return voList;
    }

    public List<LocationSalesChartVO> locationSalesChart(LocationSalesStatsParam param) {
        return aggregateMapper.locationSalesChart(param);
    }

    public List<String> searchLocationName(String locationName) {
        SiteLocationFiledDTO dto = new SiteLocationFiledDTO();
        dto.setLocationName(locationName);
        dto.setSearchFiled("tms.location_name");
        return aggregateMapper.searchFiled(dto);
    }

    public List<String> searchAgentName(String agentName) {
        SiteLocationFiledDTO dto = new SiteLocationFiledDTO();
        dto.setAgentName(agentName);
        dto.setSearchFiled("age.agent_name");
        return aggregateMapper.searchFiled(dto);
    }

    public List<String> searchSiteName(String siteName) {
        SiteLocationFiledDTO dto = new SiteLocationFiledDTO();
        dto.setSiteName(siteName);
        dto.setSearchFiled("sit.site_name");
        return aggregateMapper.searchFiled(dto);
    }


    public List<ContainerSalesStatsVO> containerSalesStats(ContainerSalesStatsParam param) {
        return aggregateMapper.containerSalesStats(param);
    }

    public List<ContainerSalesChartVO> containerSalesChart(ContainerSalesStatsParam param) {
        return aggregateMapper.containerSalesChart(param);
    }

    public List<StaffDataStatsVO> staffDataStats(StaffDataStatsParam param) {
        return aggregateMapper.staffDataStats(param);
    }
}
